package com.jic.point.controller;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.exception.AppRuntimeException;
import com.jic.point.model.entity.ConfBdPlatfromRela;
import com.jic.point.model.entity.ConfPlatform;
import com.jic.point.model.entity.ConfPlatformApply;
import com.jic.point.model.vo.request.ConfPlatformRequest;
import com.jic.point.model.vo.response.ConfFaultTreeResponse;
import com.jic.point.model.vo.response.ConfPlatformResponse;
import com.jic.point.service.ConfPlatformApplyService;
import com.jic.point.service.ConfPlatformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
/**
 * @Author: tzc
 * @date: 2020-10-11 17:28:32
 */
 @RestController
 @Slf4j
 @RequestMapping("/confPlatform")
 @Api(tags = " [子平台表]控制类-PC")
public class ConfPlatformController {
	@Autowired
	private ConfPlatformService service;

    @Autowired
    private ConfPlatformApplyService confPlatformApplyService;

    /**
      *新增记录
      * @param request 新增[子平台表]请求参数
      * @return ConfPlatformResponse 新增[子平台表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[子平台表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody ConfPlatformRequest request) throws Throwable {
     		ConfPlatform confPlatform = new ConfPlatform();
    		BeanUtils.copyProperties(request, confPlatform);
    	    long flag = service.save(confPlatform);
     return RestResult.success(flag);
    }




     /**
      * 新增或修改记录
      * @param request 新增或修改[子平台表]请求参数
      * @return ConfPlatformResponse 新增或修改[子平台表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "保存或更新[子平台表]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@RequestBody ConfPlatformRequest request) throws Throwable {
      		ConfPlatform confPlatform = new ConfPlatform();
		     BeanUtils.copyProperties(request, confPlatform);
	         long flag =   service.saveOrUpdate(confPlatform);
      return RestResult.success(flag);
     }

     /**
      * 删除记录
      * @param request 删除[子平台表]请求参数
      * @return ConfPlatformResponse 删除[子平台表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[子平台表]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody ConfPlatformRequest request) throws Throwable {
      		ConfPlatform confPlatform = new ConfPlatform();
	     	BeanUtils.copyProperties(request, confPlatform);
	     	ConfPlatform result = service.getByPrimaryKey(confPlatform);
	     	//主表逻辑删除
	     	service.deleteByPrimaryKeyLogically(confPlatform);
	     	//操作人
	     	result.setApplierId(request.getAdminOperate().getOperator());
             //插入到申请表
             ConfPlatformApply confPlatformApply = new ConfPlatformApply();
             BeanUtils.copyProperties(result, confPlatformApply);
             confPlatformApply.setDeleteFlag(0);
             confPlatformApply.setCurrentAction(2);
             long flag =   confPlatformApplyService.insertSelective(confPlatformApply);
             return RestResult.success(flag);
     }

     /**
      * 修改记录
      * @param request 修改[子平台表]请求参数
      * @return ConfPlatformResponse 修改[子平台表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[子平台表]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody ConfPlatformRequest request) throws Throwable {
      		ConfPlatform confPlatform = new ConfPlatform();
	     	BeanUtils.copyProperties(request, confPlatform);
	         long flag = service.updateByPrimaryKeySelective(confPlatform);

      return RestResult.success(flag);
     }

     /**
      * 单表查询详情信息
      * @param request 查询[子平台表]详情请求参数
      * @return ConfPlatformResponse 查询[子平台表]详情响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[子平台表]详情")
      @ResponseBody
     public RestResult<ConfPlatformResponse> getByPrimaryKey(@Validated  @RequestBody ConfPlatformRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {

	     	ConfPlatform confPlatform = new ConfPlatform();
	     	BeanUtils.copyProperties(request, confPlatform);
	     	ConfPlatform result = service.getByPrimaryKey(confPlatform);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
		     ConfPlatformResponse bo = new ConfPlatformResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new ConfPlatformResponse());
     }

    /**
      * 分页查询
      *
      * @param request 分页查询请求参数
      * @return 分页列表
      * @throws Throwable 业务异常和系统异常
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<ConfPlatform>> listPage (@RequestBody ConfPlatformRequest request) throws Throwable {
      ConfPlatform param = new ConfPlatform();
      BeanUtils.copyProperties(request, param);
      param.setDeleteFlag(0);
      param.setAuditSts(0);
     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }
     @PostMapping("/listPagefreeze")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<ConfPlatform>> listPagefreeze (@RequestBody ConfPlatformRequest request) throws Throwable {
      ConfPlatform param = new ConfPlatform();
      BeanUtils.copyProperties(request, param);
     return RestResult.success(service.listPagefreeze(param, request.getPageNum(),request.getPageSize()));
     }


      /**
       *CONF_PLATFORM 查询列表（不分页）
       * @param  request 前端请求参数
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [子平台表] ")
      @ResponseBody
      public RestResult<List<ConfPlatformResponse>>  listConfPlatformAllRecord(@RequestBody ConfPlatformRequest request) throws Throwable {
      		ConfPlatform param = new ConfPlatform();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<ConfPlatform> list = service.listAllRecord(param);
			List<ConfPlatformResponse> responseList = list.stream().map(temp -> {
			ConfPlatformResponse bo = new ConfPlatformResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }
    /**
     *CONF_PLATFORM 查询列表（不分页）
     * @param  request 前端请求参数
     */
    @PostMapping("/getPlatfromRela")
    @ApiOperation(value = "通过平台查询部门 ")
    @ResponseBody
    public RestResult<List<ConfBdPlatfromRela>>  getPlatfromRela(@RequestBody ConfPlatformRequest request) throws Throwable {
        ConfPlatform param = new ConfPlatform();
        BeanUtils.copyProperties(request, param);
        List<ConfBdPlatfromRela> list = service.getPlatfromRela(param);
        return RestResult.success(list);
    }
    /**
     * 冻结申请&&解冻申请
     */
    @PostMapping("/freezeApplication")
    @ResponseBody
    public RestResult<Long> freezeApplication(@Validated @RequestBody ConfPlatformRequest request) throws Throwable{
        //根据id查询到信息
        ConfPlatform confPlatform = new ConfPlatform();
        BeanUtils.copyProperties(request, confPlatform);
        ConfPlatform result = service.getByPrimaryKey(confPlatform);
        //主表逻辑删除为1
        service.deleteByPrimaryKeyLogically(confPlatform);
        //赋值
        result.setSts(request.getSts());
        result.setApplyReason(request.getApplyReason());
        result.setApplierId(request.getAdminOperate().getOperator());
        //插入到申请表
        ConfPlatformApply confPlatformApply = new ConfPlatformApply();
        BeanUtils.copyProperties(result, confPlatformApply);
        confPlatformApply.setCurrentAction(1);
        confPlatformApply.setAuditSts(0);
        long flag =   confPlatformApplyService.insertSelective(confPlatformApply);
        return RestResult.success(flag);
         }

    /**
     * 延期提交
     */
    @PostMapping("/delayApply")
    @ResponseBody
    public RestResult<Long> delayApply(@Validated @RequestBody ConfPlatformRequest request) throws Throwable{
        log.info("查询冻结与解冻参数入参，{}", JSONObject.toJSONString(request));
        //根据id查询到信息
        ConfPlatform confPlatform = new ConfPlatform();
        BeanUtils.copyProperties(request, confPlatform);
        ConfPlatform result = service.getByPrimaryKey(confPlatform);
        if(result.getEndTime().getTime() >= request.getEndTime().getTime()){
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "延期时间需大于活动结束时间");
        }
        //主表逻辑删除为1
        service.deleteByPrimaryKeyLogically(confPlatform);
        //赋值

        result.setEndTime(request.getEndTime());
        result.setApplyReason(request.getApplyReason());
        result.setApplierId(request.getAdminOperate().getOperator());
        //插入到申请表
        ConfPlatformApply confPlatformApply = new ConfPlatformApply();
        BeanUtils.copyProperties(result, confPlatformApply);
        confPlatformApply.setAuditSts(0);
        long flag =   confPlatformApplyService.insertSelective(confPlatformApply);
        return RestResult.success(flag);
    }
    /**
     * 提前提交
     */
    @PostMapping("/advanceApply")
    @ResponseBody
    public RestResult<Long> advanceApply(@Validated @RequestBody ConfPlatformRequest request) throws Throwable{
        log.info("查询冻结与解冻参数入参，{}", JSONObject.toJSONString(request));
        //根据id查询到信息
        ConfPlatform confPlatform = new ConfPlatform();
        BeanUtils.copyProperties(request, confPlatform);
        ConfPlatform result = service.getByPrimaryKey(confPlatform);
        if(result.getEndTime().getTime() <= request.getEndTime().getTime()){
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "提前时间需小于活动结束时间");
        }
        //主表逻辑删除为1
        service.deleteByPrimaryKeyLogically(confPlatform);
        //赋值

        result.setEndTime(request.getEndTime());
        result.setApplyReason(request.getApplyReason());
        result.setApplierId(request.getAdminOperate().getOperator());
        //插入到申请表
        ConfPlatformApply confPlatformApply = new ConfPlatformApply();
        BeanUtils.copyProperties(result, confPlatformApply);
        confPlatformApply.setAuditSts(0);
        long flag =   confPlatformApplyService.insertSelective(confPlatformApply);
        return RestResult.success(flag);
    }

    /**
     * 查询平台和活动
     * @return
     */
    @PostMapping("/listTree")
    @ResponseBody
    public RestResult<List<ConfFaultTreeResponse>> listTree(){
        List<ConfFaultTreeResponse> confFaultTreeResponses = service.selectFaultTree();

        for (ConfFaultTreeResponse confFaultTreeRespons : confFaultTreeResponses) {
            confFaultTreeRespons.setType("0");
            for (ConfFaultTreeResponse confFaultTreeResponse : confFaultTreeRespons.getChildList()) {
                confFaultTreeResponse.setType("1");
            }
        }
        return RestResult.success(confFaultTreeResponses);
    }

    /**
     * 查询平台名称和部门id用做于下拉框
     */
    @PostMapping("/queryPlatFrom")
    @ResponseBody
    public RestResult<List<ConfPlatformResponse>> queryPlatFrom()throws Throwable {
        return (RestResult<List<ConfPlatformResponse>>) service.getAlltPlatform();
    }
 }

	
